#include <SDL.h>
#include <cmath>
#include "model.h"

Model::~Model() { 
  // These deletions eliminate "still reachable"s in Valgrind  
  delete view;
  delete io;
  delete gameData;
}

void Model::makeSprites() {
   sfactory.addSprite(
    Vector2f(100,100), Vector2f(100,100), gameData->getXmlStr("lsystem"),&factory);
  int width = gameData->getXmlInt("worldWidth");
  int height = gameData->getXmlInt("worldHeight");
  int ballNumber = gameData->getXmlInt("ballNumber");
  int xPosition = 0;
  int yPosition = 0;
  int xSpeed = 0;
  int ySpeed = 0;
 srand((unsigned)time(0));
 int i = 0;
 while(i < ballNumber)
 {
	 xPosition = rand() % width;
	 yPosition = rand() % height;
	 xSpeed = rand() % width - width/2;
	 ySpeed = rand() % height - height/2;
	 if(i % 3 == 0){
		sfactory.addSprite(
		Vector2f(xPosition,yPosition),Vector2f(xSpeed,ySpeed), gameData->getXmlStr("redFile"),&factory);
	 } else if(i % 3 == 1){
		sfactory.addSprite(
		Vector2f(xPosition,yPosition),Vector2f(xSpeed,ySpeed), gameData->getXmlStr("greenFile"),&factory);
	 } else if(i % 3 == 2){
		sfactory.addSprite(
		Vector2f(xPosition,yPosition),Vector2f(xSpeed,ySpeed), gameData->getXmlStr("blueFile"),&factory);
	 }
	 i++;
}
  //Frame* frame = factory.getFrame(gameData->getXmlStr("blueFile"),50, 50, 0, 0, true);
  //for (int i = 0; i < MAX_ORBS; ++i) {
    //int x = rand() % width;
    //int y = rand() % height;
    //int xspeed = (rand() % 200 + 50) * (rand()%2?1:-1);
    //int yspeed = (rand() % 200 + 50) * (rand()%2?1:-1);
    //sfactory.addSprite( 
       //new Sprite( Vector2f(x, y), Vector2f(xspeed, yspeed),
       //frame) 
    //);
  //}
}

void Model::changeTracker() {
	if(sprites.size()<4){
		trackingNumber = (trackingNumber + 1) % sprites.size();
	} else {
		trackingNumber = (trackingNumber + 1) % 4;
	}
	  view->setObjectToTrack(sprites[trackingNumber]);
}

Model::Model() :

  gameData(Gamedata::getInstance()),
  io(IOManager::getInstance()),
  clock(),
  trackingNumber(0),  screen( io->getScreen() ),
  factory(),
  sfactory(),
  world(factory.getFrame(gameData->getXmlStr("background"),
        gameData->getXmlInt("worldWidth"), gameData->getXmlInt("worldHeight"), 0, 0, false)),
  view(Viewport::getInstance()),	
  sprites(),
  showHelp(false)
  {
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
      throw string("Unable to initialize SDL: ");
    }
    SDL_putenv(const_cast<char*>("SDL_VIDEO_CENTERED=center"));
    makeSprites();
    sprites = sfactory.getSprites();
    view->setObjectToTrack(sprites[0]);
    atexit(SDL_Quit);
    SDL_WM_SetCaption( gameData->getXmlStr("appName").c_str(), NULL );
}

void Model::draw() const {
  world.draw();
  for (unsigned i = 0; i < sprites.size(); ++i) {
    sprites[i]->draw();
  }
 if(showHelp) {
	drawHelp();
 }
  drawFps();
  drawFocus();	
  SDL_Flip(screen);
}

void Model::update() {
  view->update();
  world.update();
  unsigned ticks = clock.getElapsedTicks();
  for (unsigned i = 0; i < sprites.size(); ++i) {
    sprites[i]->update(ticks);
  }
  clock++;
}

void Model::pause(){
	//std::cout<<"call pause"<<std::endl;
	if(!clock.isPaused()){
		clock.pause();
	} else {
		clock.unpause();
	}
	//std::cout<<clock.isPaused()<<std::endl;
}

bool Model::isPaused() const {
	return clock.isPaused();
}

void Model::drawHelp() const {
	SDL_Color color = {255,255,0,0};
	io->printMessageAt("Help memu", 10, 10,color);
	io->printMessageAt("1.Press <space> to switch the sprite", 10, 30,color);
	io->printMessageAt("2.Press <p> to pause or resume the game", 10, 50,color);
}

void Model::drawFps() const {
	SDL_Color color = {255,255,0,0};
	//clock.debug();
	io->printMessageValueAt("Fps: ",clock.getFps(),gameData->getXmlInt("viewWidth")*3/4, 10,color);

}

void Model::drawFocus() const {
		if(trackingNumber == 0){
			string focus = "L-System";
			SDL_Color color = {255,255,255,0};
			io->printMessageAt("Focus: "+focus,gameData->getXmlInt("viewWidth")*3/4, 30,color);
		} else if(trackingNumber == 1){
			string focus = "Red Ball";
			SDL_Color color = {255,0,0,0};
			io->printMessageAt("Focus: "+focus,gameData->getXmlInt("viewWidth")*3/4, 30,color);
		} else if(trackingNumber == 2){
			string focus = "Green Ball";
			SDL_Color color = {0,255,0,0};
			io->printMessageAt("Focus: "+focus,gameData->getXmlInt("viewWidth")*3/4, 30,color);
		} else if(trackingNumber == 3){
			string focus = "Blue Ball";
			SDL_Color color = {0,0,255,0};
			io->printMessageAt("Focus: "+focus,gameData->getXmlInt("viewWidth")*3/4, 30,color);
		} 
}

